Class and Objects
Program to find the sum of 2 nos using class
class abc
{
int a, b, c;
public void get()
{
Console.WriteLine("Enter a,b values");
a = int.Parse(Console.ReadLine());
b = int.Parse(Console.ReadLine());
}
public void put()
{
Console.WriteLine("Sum" + (a + b));
}
}
class sample
{
static void Main(string[] args)
{
abc x = new abc();
x.get();
x.put();
}
}
Program to find the sum of 2 nos by passing arguments
class abc
{
int a, b, c;
public void get(int x,int y)
{
a = x;
b = y;
}
public void put()
{
Console.WriteLine("Sum" + (a + b));
}
}
class sample
{
static void Main(string[] args)
{
abc x = new abc();
x.get(32,44);
x.put();
}
}
Find the sum of 2nos by return values
using System;
class abc
{
int a, b ;
public void get(int x, int y)
{
a = x;
b = y;
}
public int put()
{
return (a * b);
}
}
class sample
{
static void Main(string[] args)
{
abc x = new abc();
x.get(32, 44);
int c=x.put();
Console.WriteLine("Sum" + c);
}
}
Note: Do more programs on class and objects
Program to find the sum of the numbers using objects as an arguments
using System;
class abc
{
int a, b ;
public void get(int x, int y)
{
a = x;
b = y;
}
public void put()
{
Console.WriteLine("Sum" + (a + b));
}
public void print(abc x,abc y)
{
Console.WriteLine ("Grand Sum"+(x.a+x.b+y.a+y.b));
}
}
class sample
{
static void Main(string[] args)
{
abc x= new abc();
abc y= new abc();
abc z= new abc();
x.get(32, 44);
x.put();
y.get(4, 2);
y.put();
z.print(x, y);
}
}
Program to find the sum of the numbers using return object.
using System;
class abc
{
int a, b ;
public void get(int x, int y)
{
a = x;
b = y;
}
public void put()
{
Console.WriteLine("Sum" + (a + b));
}
public abc print(abc x,abc y)
{
abc k = new abc();
k.a = x.a + y.b;
k.b = x.b + y.b;
return (k);
}
}
class sample
{
static void Main(string[] args)
{
abc x= new abc();
abc y= new abc();
abc z= new abc();
x.get(32, 44);
x.put();
y.get(4, 2);
y.put();
z=z.print(x, y);
z.put();
}
}
Note: Do more programs on objects as an orguments and return objects
(grand total and avg marks of 2 students, grand net amout of 2 products, grand net salary of 2 emplyees)
program on object arrays to find the total and average marks of 5 students
using System;
class student
{
int m, p, c, tot, avg;
public void get()
{
Console.WriteLine("Etner maths phy chem");
m = Convert.ToInt32(Console.ReadLine());
p = Convert.ToInt32(Console.ReadLine());
c = Convert.ToInt32(Console.ReadLine());
tot = m + p + c;
avg = tot / 3;
}
public void put()
{
Console.WriteLine (m+" "+p+" "+c+" "+tot+" "+avg );
}
}
class sample
{
static void Main(string[] args)
{
student[] x = new student[50];
int n;
Console.WriteLine("Enter n value");
n = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < n; i++)
{
x[i]=new student() ;
x[i].get();
}
for (int i = 0; i < n; i++)
{
x[i].put();
}
}
}
This: This is a keyword, used in a class, is a reference to the current instance.
Program to find the sum of 2 nos using this
using System;
class abc
{
int a, b;
public void get(int a, int b)
{
this.a = a;
this.b = b;
}
public void put()
{
Console.WriteLine("Sum of 2 nos" + (a + b));
}
}
class sample
{
public static void Main()
{
abc x = new abc();
x.get(5, 6);
x.put();
}
}
Program to find the big number out of 2 numbers using this
using System;
class abc
{
int a;
public void get(int a)
{
this.a = a;
}
public void put()
{
Console.WriteLine("Big number" + a);
}
public abc print(abc x)
{
if (x.a > a)
return x;
else
return this;
}
}
class sample
{
public static void Main()
{
abc x = new abc();
abc y = new abc();
abc z;
x.get(5);
y.get(4);
z = x.print(y);
z.put();
}
}